package component
{
	import events.CommonEvent;
	
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	import mx.controls.Alert;
	import mx.core.IVisualElement;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.events.ResizeEvent;
	import mx.events.ToolTipEvent;
	
	import spark.components.Group;
	import spark.components.Label;
	import spark.components.TileGroup;
	import spark.formatters.DateTimeFormatter;
	import mx.core.IFlexModuleFactory;
	import mx.styles.CSSStyleDeclaration;
	import gzx.specialcomponents.CalendarDayItem;
	import gzx.specialcomponents.CalendarInfo;
	import gzx.specialcomponents.CityDisplayLabel;
	
	[Event(name="LabelClick", type="events.CommonEvent")]
	public class Calendar extends Group
	{
		private var dtfDay:DateTimeFormatter = new DateTimeFormatter();
		private var weekArray:Array = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
		private var _displayedYear:int = -1;
		private var _displayedMonth:int = -1;
		private var dayBackText:String = "";
		private var calendarInfos:Array = new Array();
		private var dicDayLocation:Dictionary = new Dictionary();
		private var eachWidth:Number = 190;
		private var eachHeight:Number = 100;
		private var __moduleFactoryInitialized:Boolean = false;
		
		public function Calendar()
		{
			super();
			this.percentWidth = 100.0;
			this.percentHeight = 100.0;
			this.minHeight = 400;
			this.minWidth = 1260;
			_Calendar_DateTimeFormatter1_i();
			this.addEventListener("resize", group1_resizeHandler);
		}
		
		private function _Calendar_DateTimeFormatter1_i():void
		{
			dtfDay.dateTimePattern = "yyyy-MM-dd";
			dtfDay.errorText = "Invalid input value";
		}
		
		override public function set moduleFactory(factory:IFlexModuleFactory):void
		{
			super.moduleFactory = factory;
			if (__moduleFactoryInitialized)
				return;
			__moduleFactoryInitialized = true;

			if (!this.styleDeclaration)
			{
				this.styleDeclaration = new CSSStyleDeclaration(null, styleManager);
			}
			
			this.styleDeclaration.defaultFactory = function():void
			{
				this.fontFamily = "微软雅黑";
			};
		}
		
		public function set displayedYear(value:int):void
		{
			_displayedYear = value;
			if(_displayedYear>0)
			{
				draw();
			}
		}
		public function set displayedMonth(value:int):void
		{
			_displayedMonth = value;
			if(_displayedMonth >0)
			{
				draw();
			}
		}
		public function SetDisplayYearMonth(year:int,month:int):void
		{
			_displayedYear = year;
			_displayedYear = month;
			if(_displayedYear >0 && _displayedMonth>0)
			{
				draw();
			}
		}
		
		public function set CalendarInfos(value:Array):void
		{
			calendarInfos = value;
			drawInfos();
		}
		
		public function set DayBackText(value:String):void
		{
			dayBackText = value;
			setDayBackText();
		}
		
		protected function draw():void
		{
			this.removeAllElements();
			this.graphics.clear();
			if(_displayedYear<0 || _displayedMonth<1 || _displayedMonth>12) return;
			var contentwidth:int = this.width>this.minWidth ? this.width-10 : this.minWidth-10;
			var contentheight:int = this.height>this.minHeight ? this.height-10 : this.minHeight -10;
			var date:Date = new Date(_displayedYear,_displayedMonth,0);
			var monthday:int = date.getDate();
			var firtDay:Date = new Date(_displayedYear,_displayedMonth-1,1);
			var firstDayInWeek:int = firtDay.day;
			var weekLength:int = Math.floor((monthday-1 + firstDayInWeek) / 7);
			eachWidth = contentwidth/7;
			eachHeight = (contentheight-23)/(weekLength+1);
			//画星期背景
			var weekBackUI:UIComponent = new UIComponent();
			weekBackUI.graphics.beginFill(0xE3EEF5);
			weekBackUI.graphics.drawRect(5,5,contentwidth,23);
			weekBackUI.graphics.endFill();
			this.addElement(weekBackUI);
			
			var lineUI:UIComponent = new UIComponent();
			lineUI.graphics.lineStyle(1,0x696969);
			lineUI.graphics.moveTo(5,5);
			lineUI.graphics.lineTo(contentwidth + 5,5);//画星期
			for (var j:int=0;j<8;j++)//画竖线
			{
				var labelWeek:Label = new Label();
				labelWeek.setStyle("fontSize", 14);
				labelWeek.setStyle("textAlign", "center");
				labelWeek.setStyle("fontWeight", "bold");
				labelWeek.text = weekArray[j] as String;
				labelWeek.x = eachWidth*j+5;
				labelWeek.y = 10;
				labelWeek.width = eachWidth;
				this.addElement(labelWeek);
				
				lineUI.graphics.moveTo(eachWidth*j+5, 5);
				lineUI.graphics.lineTo(eachWidth*j+5, contentheight + 5);
			}
			
			for (var i:int=0;i<weekLength+2;i++)//画横线
			{
				lineUI.graphics.moveTo(5, eachHeight*i+28);
				lineUI.graphics.lineTo(contentwidth+5,eachHeight*i+28);
			}
			this.addElement(lineUI);
			
			for (var k:int=1;k<=monthday;k++)
			{
				var dayitem:CalendarDayItem = new CalendarDayItem();
				var x:int = Math.floor((k-1 + firstDayInWeek) % 7)*eachWidth + 5;
				var y:int = Math.floor((k-1 + firstDayInWeek) / 7)*eachHeight + 28;
				dayitem.x = x;
				dayitem.y = y;
				dayitem.width = eachWidth;
				dayitem.height = eachHeight;
				dayitem.addEventListener(MouseEvent.MOUSE_OVER,dayItem_MouseOver);
				dayitem.addEventListener(MouseEvent.MOUSE_OUT,dayItem_MouseOut);
				dayitem.Day = k;
				this.addElement(dayitem);
				var dateString:String = dtfDay.format(new Date(_displayedYear,_displayedMonth-1,k));
				dicDayLocation[dateString] = dayitem;//存放天
			}
		}
		
		private function removeAllDayEle():void
		{
			for (var key:String in dicDayLocation)
			{
				var dayitem:CalendarDayItem = dicDayLocation[key] as CalendarDayItem;
				var dayItemLen:int = dayitem.numElements;
				for(var i:int=dayItemLen-1; i>-1; i--)
				{
					if(dayitem.getElementAt(i) is Label)
					{
						var label:Label = dayitem.getElementAt(i) as Label;
						if(label.id !="lblDay" && label.id !="lblBackText")
						{
							dayitem.removeElementAt(i);
						}
					}
					else
					{
						dayitem.removeElementAt(i);
					}
				}
			}
		}
		
		private var overRect:UIComponent = null;
		private function dayItem_MouseOver(event:MouseEvent):void
		{
			var dayitem:CalendarDayItem = null;
			if(event.target is CalendarDayItem)
			{
				dayitem = event.target as CalendarDayItem;
			}
			else if(event.target.parent is CalendarDayItem)
			{
				dayitem = event.target.parent as CalendarDayItem;
			}
			if(dayitem !=null)
			{
				this.graphics.beginFill(0xFFC0C0);
				this.graphics.moveTo(dayitem.x ,dayitem.y);
				this.graphics.lineTo(dayitem.x + eachWidth, dayitem.y);
				this.graphics.lineTo(dayitem.x + eachWidth,eachHeight + dayitem.y);
				this.graphics.lineTo(dayitem.x, eachHeight + dayitem.y);
				this.graphics.lineTo(dayitem.x, dayitem.y);
				this.graphics.endFill();
			}
		}
		
		private function setDayBackText():void
		{
			for (var dateString:String in dicDayLocation)
			{
				var dayitem:CalendarDayItem = dicDayLocation[dateString] as CalendarDayItem;
				if(dayitem!=null)
				{
					dayitem.BackText = dayBackText;
				}
			}
		}
		
		private function dayItem_MouseOut(event:MouseEvent):void
		{
			this.graphics.clear();
		}
		
		private function drawInfos():void
		{
			removeAllDayEle();
			if(calendarInfos !=null)
			{
				for (var i:int=0;i<calendarInfos.length;i++)
				{
					var calendarInfo:CalendarInfo = calendarInfos[i] as CalendarInfo;
					if(calendarInfo!=null)
					{
						var key:String = dtfDay.format(calendarInfo.CalDate);
						var dayitem:CalendarDayItem = dicDayLocation[key] as CalendarDayItem;
						if(dayitem!=null)
						{
							if(calendarInfo.BackgroundColor >0)
							{
								var backgroundui:UIComponent = new UIComponent();
								backgroundui.alpha =0.75;
								backgroundui.graphics.beginFill(calendarInfo.BackgroundColor);
								backgroundui.graphics.moveTo(1 ,1);
								backgroundui.graphics.lineTo(eachWidth,1);
								backgroundui.graphics.lineTo(eachWidth, eachHeight);
								backgroundui.graphics.lineTo(1, eachHeight);
								backgroundui.graphics.lineTo(1, 1);
								backgroundui.graphics.endFill();
								dayitem.addElement(backgroundui);
								dayitem.setElementIndex(backgroundui,0);
							}
							if(calendarInfo.TextArray!=null)
							{
								for (var j:int=0; j<calendarInfo.TextArray.length;j++)
								{
									if(calendarInfo.TextArray[j] is IVisualElement)
									{
										var ele:IVisualElement = calendarInfo.TextArray[j] as IVisualElement;
										if(ele.percentWidth ==100)
										{
											ele.width = eachWidth-8;
										}
										dayitem.addElement(calendarInfo.TextArray[j]);
									}
									else if(calendarInfo.TextArray[j] is CityDisplayLabel)
									{
										var cityDisplayLabel:CityDisplayLabel = calendarInfo.TextArray[j] as CityDisplayLabel;
										var vgap:int = (eachHeight -64) / 5;
										var hgap:int = (eachWidth - 168) / 7;
										var index:int = cityDisplayLabel.DisplayIndex;
										var labelx:int = 3;
										var labely:int = vgap;
										if(index>0 && index<5)
										{
											labelx = (index+1)*28 + (index+2)*hgap;
										}
										else if(index>4 && index<11)
										{
											labelx = (index-5)*28 + (index-4)*hgap;
											labely = vgap*2 + 16;
										}
										else if(index>10 && index<17)
										{
											labelx = (index-11)*28 + (index-10)*hgap;
											labely = vgap*3 + 32;
										}
										else if(index>16)
										{
											labelx = (index-17)*28 + (index-16)*hgap;
											labely = vgap*4 + 48;
										}
										var label:Label = new Label();
										label.x = labelx;
										label.y = labely;
										label.text = cityDisplayLabel.CityName;
										label.setStyle("color",0xFFFFFF);
										label.setStyle("fontSize",12);
										label.setStyle("backgroundColor",cityDisplayLabel.Color);
										label.setStyle("paddingBottom",1);
										label.setStyle("paddingLeft",2);
										label.setStyle("paddingRight",2);
										label.setStyle("paddingTop",2);
										label.buttonMode = true;
										label.addEventListener(MouseEvent.CLICK,calendar_LabelClickHandler);
										dayitem.addElement(label);
									}
								}
							}
							dayitem.DayColor = calendarInfo.DayColor;
							dayitem.DaySize = calendarInfo.DaySize;
						}
					}
				}
			}
		}
		
		protected function calendar_LabelClickHandler(event:MouseEvent):void
		{
			var selText:String = event.target.text as String;
			var eventObject:CommonEvent = new CommonEvent("LabelClick",selText);
			dispatchEvent(eventObject);
		}
		
		protected function group1_resizeHandler(event:ResizeEvent):void
		{
			draw();
			drawInfos();
			setDayBackText();
		}
	}
}